"""
class Foo(object):
    def find(self):
        print("我是绑定对象的方法")

print(Foo) #打印类
obj = Foo()
print(obj) # 打印对象

obj.find()
print(type(obj)) # <__main__.Foo object at 0x0198EE50>
# 按住ctrl + 鼠标左键点击
print(str)

name = str("jiac")
print(name)
print(type(name)) # <class 'str'>
"""
"""
import pymysql
# 
# :param host: 就是阿里云服务器的ip地址
# :param user: 数据库的账号名  一般都是root
# :param password: 数据库密码
# :param database: 数据库的名字
# :param cursorclass:  返回的格式是字典格式，有key和value

# 使用pymysq连接
db = pymysql.connect(host = "47.104.65.165",user = 'root',password='123456',database='jiac',cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor() # 游标对象，每次执行sql的时候，都需要用到

#新增
cursor.execute(f'insert into student(name,classid) values("关博文","1404")')
db.commit()

# 修改
cursor.execute("update student set name='关博文plus' where name='关博文'")
db.commit()


# 删除
cursor.execute('delete from student where name = "关博文plus"')
db.commit()

# 查询
cursor.execute("select * from student") # 使用游标对象执行sql语句
print(cursor.fetchall()) # 查询这个表符合sql条件的全部数据
# print(cursor.fetchone()) # 查询这个表符合sql条件的一条数据

cursor.close() # 用完记得关闭，先关闭  游标
db.close() # 再关闭数据库
"""
# 用类来实现pymysq封装
import pymysql
class DB:
    def __init__(self,host,user,password,database,cursorclass):
        """
        :param host: 就是阿里云服务器的ip地址
        :param user: 数据库的账号名  一般都是root
        :param password: 数据库密码
        :param database: 数据库的名字
        :param cursorclass:  返回的格式是字典格式，有key和value
        """
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.cursorclass = cursorclass
    def connect(self):
        # 创建数据库对象
        self.db = pymysql.connect(host = self.host,user = self.user,password = self.password,database=self.database,cursorclass=self.cursorclass)
        # 创建游标对象
        self.cursor = self.db.cursor()
    def close(self):
        self.cursor.close() # 先关闭游标
        self.db.close() # 再关闭数据库对象
    def select_all(self,sql): # 查询全部的数据
        result = 0
        try:
            self.connect() # 先连接
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            self.close()
            return result
        except Exception as ec:
            self.db.rollback() #如果sql执行错误，先回滚
            print("数据库sql有误%s"%ec)
            return result
    def select_one(self,sql):
        result = 0
        try:
            self.connect()  # 先连接
            self.cursor.execute(sql)
            result = self.cursor.fetchone()
            self.close()
            return result
        except Exception as ec:
            self.db.rollback()  # 如果sql执行错误，先回滚
            print("数据库sql有误%s" % ec)
            return result
    def __edit(self,sql): #主程序
        self.connect()
        try:
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
            result = True
            return result
        except Exception as ec:
            self.db.rollback() # 如果sql执行错误，回滚
            print("数据库sql有误%s"%ec)
            result = False
            return result
    def insert(self,sql):
        return self.__edit(sql)
    def update(self,sql):
        return self.__edit(sql)
    def delete(self,sql):
        return self.__edit(sql)
# 通过DB类，实例化db1对象
db1 = DB(host = "47.104.65.165",user = 'root',password='123456',database='jiac',cursorclass=pymysql.cursors.DictCursor)
db1.insert('insert into student values(1,"关博文","1704")')
print(db1.select_all('select * from student'))











